<template>
  <div class="sidebar-login-wrap">
    <div class="sidebar-container">
      <!-- 左侧品牌区域 -->
      <div class="brand-side">
        <div class="brand-content">
          <img src="@/assets/logo.png" alt="Logo" class="brand-logo" />
          <h1>ERP管理系统</h1>
          <p class="brand-subtitle">智能 · 高效 · 协同</p>
          <div class="brand-features">
            <div class="feature-item">
              <el-icon><CircleCheck /></el-icon>
              <span>安全可靠</span>
            </div>
            <div class="feature-item">
              <el-icon><CircleCheck /></el-icon>
              <span>功能强大</span>
            </div>
            <div class="feature-item">
              <el-icon><CircleCheck /></el-icon>
              <span>易于使用</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录区域 -->
      <div class="login-side">
        <div class="login-content">
          <h2>欢迎回来</h2>
          <p class="login-subtitle">请登录您的账户</p>

          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            @submit.prevent="handleLogin"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="用户名"
                size="large"
                prefix-icon="User"
                class="sidebar-input"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="密码"
                size="large"
                prefix-icon="Lock"
                show-password
                @keyup.enter="handleLogin"
                class="sidebar-input"
              />
            </el-form-item>

            <el-form-item>
              <div class="form-options">
                <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
                <el-link type="primary" :underline="false" @click="showForgotPassword">
                  忘记密码？
                </el-link>
              </div>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                size="large"
                class="sidebar-btn"
                :loading="loading"
                @click="handleLogin"
                style="width: 100%"
              >
                {{ loading ? '登录中...' : '登录' }}
              </el-button>
            </el-form-item>
          </el-form>

          <div class="login-footer">
            <p>默认账号：<strong>admin</strong> / <strong>123456</strong></p>
          </div>
        </div>
      </div>
    </div>

    <!-- 忘记密码对话框 -->
    <ForgotPasswordDialog
      v-model="forgotPasswordVisible"
      @success="handleResetSuccess"
    />
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { CircleCheck } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import ForgotPasswordDialog from './components/ForgotPasswordDialog.vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const emit = defineEmits(['login'])

const loginFormRef = ref()
const loading = ref(false)
const forgotPasswordVisible = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: '123456',
  rememberMe: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    const response = await userStore.login(loginForm)

    if (response.code === 200) {
      ElMessage.success(response.msg || '登录成功')
      emit('login', response)
      
      const redirect = route.query.redirect
      if (redirect && redirect !== '/login') {
        router.push(decodeURIComponent(redirect))
      } else {
        router.push('/dashboard')
      }
    } else {
      ElMessage.error(response.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '网络请求失败，请重试')
  } finally {
    loading.value = false
  }
}

const showForgotPassword = () => {
  forgotPasswordVisible.value = true
}

const handleResetSuccess = () => {
  forgotPasswordVisible.value = false
}
</script>

<style scoped>
.sidebar-login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
}

.sidebar-container {
  display: flex;
  width: 100%;
  max-width: 1200px;
  min-height: 600px;
  background: #fff;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  overflow: hidden;
}

.brand-side {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  color: #fff;
}

.brand-content {
  text-align: center;
}

.brand-logo {
  height: 60px;
  margin-bottom: 24px;
  filter: brightness(0) invert(1);
}

.brand-side h1 {
  margin: 0 0 12px;
  font-size: 32px;
  font-weight: 700;
}

.brand-subtitle {
  margin: 0 0 40px;
  font-size: 16px;
  opacity: 0.9;
}

.brand-features {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 40px;
}

.feature-item {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 16px;
}

.login-side {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
}

.login-content {
  width: 100%;
  max-width: 400px;
}

.login-content h2 {
  margin: 0 0 8px;
  font-size: 28px;
  font-weight: 700;
  color: #303133;
}

.login-subtitle {
  margin: 0 0 32px;
  color: #909399;
  font-size: 14px;
}

.login-form {
  margin-top: 24px;
}

.sidebar-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  padding: 12px 16px;
}

.sidebar-input :deep(.el-input__wrapper):hover {
  border-color: #c0c4cc;
}

.sidebar-input :deep(.el-input__wrapper.is-focus) {
  border-color: #409eff;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.sidebar-btn {
  border-radius: 8px;
  font-weight: 600;
  height: 48px;
}

.login-footer {
  text-align: center;
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid #e4e7ed;
}

.login-footer p {
  margin: 0;
  color: #909399;
  font-size: 12px;
}

@media (max-width: 768px) {
  .sidebar-container {
    flex-direction: column;
  }
  
  .brand-side {
    padding: 40px;
  }
  
  .login-side {
    padding: 40px;
  }
}
</style>

